package com.mlethe.library.socialtool.core;

import android.app.Activity;
import android.os.Bundle;

import com.mlethe.library.socialtool.callback.ShareActionListener;
import com.mlethe.library.socialtool.entity.ShareAudio;
import com.mlethe.library.socialtool.entity.ShareImage;
import com.mlethe.library.socialtool.entity.ShareMiniProgram;
import com.mlethe.library.socialtool.entity.ShareWeb;
import com.tencent.tauth.IUiListener;
import com.tencent.tauth.UiError;

import static com.tencent.connect.share.QQShare.SHARE_TO_QQ_AUDIO_URL;
import static com.tencent.connect.share.QQShare.SHARE_TO_QQ_IMAGE_LOCAL_URL;
import static com.tencent.connect.share.QQShare.SHARE_TO_QQ_IMAGE_URL;
import static com.tencent.connect.share.QQShare.SHARE_TO_QQ_KEY_TYPE;
import static com.tencent.connect.share.QQShare.SHARE_TO_QQ_MINI_PROGRAM;
import static com.tencent.connect.share.QQShare.SHARE_TO_QQ_MINI_PROGRAM_APPID;
import static com.tencent.connect.share.QQShare.SHARE_TO_QQ_MINI_PROGRAM_PATH;
import static com.tencent.connect.share.QQShare.SHARE_TO_QQ_MINI_PROGRAM_TYPE;
import static com.tencent.connect.share.QQShare.SHARE_TO_QQ_SUMMARY;
import static com.tencent.connect.share.QQShare.SHARE_TO_QQ_TARGET_URL;
import static com.tencent.connect.share.QQShare.SHARE_TO_QQ_TITLE;
import static com.tencent.connect.share.QQShare.SHARE_TO_QQ_TYPE_AUDIO;
import static com.tencent.connect.share.QQShare.SHARE_TO_QQ_TYPE_DEFAULT;
import static com.tencent.connect.share.QQShare.SHARE_TO_QQ_TYPE_IMAGE;

/**
 * QQ分享工具类
 *
 * @author Mlethe
 */
public class QQShare {

    private static ShareActionListener mListener;

    private static ShareCallback mShareCallback = new ShareCallback();

    public static void setShareActionListener(ShareActionListener listener) {
        mListener = listener;
    }

    /**
     * 分享网页
     *
     * @param share    支持图片URL或本地路径
     * @param activity
     */
    public static void share(QQ qq, ShareWeb share, Activity activity) {
        if (!qq.isAppInstalled()) {
            if (mListener != null) {
                mListener.onNotInstalled(Platform.QQ);
                mListener = null;
            }
            return;
        }
        Bundle params = new Bundle();
        params.putInt(SHARE_TO_QQ_KEY_TYPE, SHARE_TO_QQ_TYPE_DEFAULT);
        params.putString(SHARE_TO_QQ_TITLE, share.getTitle());
        params.putString(SHARE_TO_QQ_SUMMARY, share.getDesc());
        params.putString(SHARE_TO_QQ_TARGET_URL, share.getUrl());
        if (share.getImageUrl() != null && !"".equals(share.getImageUrl())) {
            params.putString(SHARE_TO_QQ_IMAGE_URL, share.getImageUrl());
        } else if (share.getImagePath() != null && !"".equals(share.getImagePath())) {
            params.putString(SHARE_TO_QQ_IMAGE_LOCAL_URL, share.getImagePath());
        }
        qq.getApi().shareToQQ(activity, params, mShareCallback);
    }

    /**
     * 分享图片
     *
     * @param share    只支持本地图片
     * @param activity
     */
    public static void share(QQ qq, ShareImage share, Activity activity) {
        if (!qq.isAppInstalled()) {
            if (mListener != null) {
                mListener.onNotInstalled(Platform.QQ);
                mListener = null;
            }
            return;
        }
        Bundle params = new Bundle();
        params.putInt(SHARE_TO_QQ_KEY_TYPE, SHARE_TO_QQ_TYPE_IMAGE);
        if (share.getImagePath() != null && !"".equals(share.getImagePath())) {
            params.putString(SHARE_TO_QQ_IMAGE_LOCAL_URL, share.getImagePath());
        }
        qq.getApi().shareToQQ(activity, params, mShareCallback);
    }

    /**
     * 分享音乐
     *
     * @param share    支持图片URL或本地路径
     * @param activity
     */
    public static void share(QQ qq, ShareAudio share, Activity activity) {
        if (!qq.isAppInstalled()) {
            if (mListener != null) {
                mListener.onNotInstalled(Platform.QQ);
                mListener = null;
            }
            return;
        }
        Bundle params = new Bundle();
        params.putInt(SHARE_TO_QQ_KEY_TYPE, SHARE_TO_QQ_TYPE_AUDIO);
        params.putString(SHARE_TO_QQ_TITLE, share.getTitle());
        params.putString(SHARE_TO_QQ_SUMMARY, share.getDesc());
        params.putString(SHARE_TO_QQ_TARGET_URL, share.getAudioUrl());
        if (share.getImageUrl() != null && !"".equals(share.getImageUrl())) {
            params.putString(SHARE_TO_QQ_IMAGE_URL, share.getImageUrl());
        } else if (share.getImagePath() != null && !"".equals(share.getImagePath())) {
            params.putString(SHARE_TO_QQ_IMAGE_LOCAL_URL, share.getImagePath());
        }
        params.putString(SHARE_TO_QQ_AUDIO_URL, share.getAudioDataUrl());
        qq.getApi().shareToQQ(activity, params, mShareCallback);
    }

    /**
     * 分享小程序
     *
     * @param share    支持图片URL或本地路径
     * @param activity
     */
    public static void share(QQ qq, ShareMiniProgram share, Activity activity) {
        if (!qq.isAppInstalled()) {
            if (mListener != null) {
                mListener.onNotInstalled(Platform.QQ);
                mListener = null;
            }
            return;
        }
        Bundle params = new Bundle();
        params.putInt(SHARE_TO_QQ_KEY_TYPE, SHARE_TO_QQ_MINI_PROGRAM);
        params.putString(SHARE_TO_QQ_MINI_PROGRAM_APPID, share.getUserName());
        params.putString(SHARE_TO_QQ_MINI_PROGRAM_PATH, share.getPath());
        // 3表示正式版，1表示体验版
        if (share.getType() == 0) {
            params.putInt(SHARE_TO_QQ_MINI_PROGRAM_TYPE, 3);
        } else if (share.getType() == 2) {
            params.putInt(SHARE_TO_QQ_MINI_PROGRAM_TYPE, 1);
        }
        params.putString(SHARE_TO_QQ_TITLE, share.getTitle());
        params.putString(SHARE_TO_QQ_SUMMARY, share.getDesc());
        params.putString(SHARE_TO_QQ_TARGET_URL, share.getWebpageUrl());
        if (share.getImageUrl() != null && !"".equals(share.getImageUrl())) {
            params.putString(SHARE_TO_QQ_IMAGE_URL, share.getImageUrl());
        } else if (share.getImagePath() != null && !"".equals(share.getImagePath())) {
            params.putString(SHARE_TO_QQ_IMAGE_LOCAL_URL, share.getImagePath());
        }
        qq.getApi().shareToQQ(activity, params, mShareCallback);
    }

    /**
     * 回调处理类
     */
    private static class ShareCallback implements IUiListener {
        @Override
        public void onComplete(Object o) {
            if (mListener != null) {
                mListener.onComplete(Platform.QQ);
                mListener = null;
            }
        }

        @Override
        public void onError(UiError uiError) {
            if (mListener != null) {
                mListener.onFail(Platform.QQ);
                mListener = null;
            }
        }

        @Override
        public void onCancel() {
            if (mListener != null) {
                mListener.onCancel(Platform.QQ);
                mListener = null;
            }
        }
    }
}
